package com.nb.service.impl;


import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.nb.dto.CreateOrUpdateAclDTO;
import com.nb.entity.SysAclEntity;
import com.nb.mapper.SysAclMapper;
import com.nb.service.ISysAclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 权限表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class SysAclServiceImpl extends ServiceImpl<SysAclMapper, SysAclEntity> implements ISysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Override
    public void createAcl(CreateOrUpdateAclDTO createOrUpdateAclDTO) {
        SysAclEntity sysAclEntity = new SysAclEntity();
        sysAclEntity.setName(createOrUpdateAclDTO.getName());
        sysAclEntity.setCode(createOrUpdateAclDTO.getCode());
        sysAclEntity.setUrl(createOrUpdateAclDTO.getUrl());
        sysAclEntity.setCreatedTime(new Date(System.currentTimeMillis()));
        sysAclMapper.insertSelective(sysAclEntity);
    }

    @Override
    public void updateAcl(CreateOrUpdateAclDTO createOrUpdateAclDTO) {

        SysAclEntity sysAclEntity = sysAclMapper.selectOneById(createOrUpdateAclDTO.getId());
        sysAclEntity.setName(createOrUpdateAclDTO.getName());
        sysAclEntity.setCode(createOrUpdateAclDTO.getCode());
        sysAclEntity.setUrl(createOrUpdateAclDTO.getUrl());
        sysAclEntity.setUpdatedTime(new Date(System.currentTimeMillis()));
        sysAclMapper.update(sysAclEntity,false);
    }

    @Override
    public List<SysAclEntity> queryByName(String name) {
        List<SysAclEntity> list;
        if (name != null && !name.trim().equals("undefined")) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.like(SysAclEntity::getName,name);
            list = sysAclMapper.selectListByQuery(queryWrapper);
        }else{//查询所有
            list = sysAclMapper.selectListByQuery(new QueryWrapper());
        }
        return list;
    }
}